
/**
 * 刷新按钮状态，0 不可用；1 可用
 * 数据格式 {btnAdd：{id:buttonId1,status: 1 },btnEdit：{id:buttonId2,status: 0}}
 * @param btns
 */
function freshButtonStatus(btns){
	if(btns == null){return ;}
	for(var i in btns){
		var btn = btns[i];
		var btnObj = jQuery('#'+btn.id);
		if(btn.st == 0){
			setButtonDisAble(btnObj);
		}else{
			setButtonEnable(btnObj);
		}
	}
}

/**
 * 设置按钮不可用
 * @param btnObj
 */
function setButtonDisAble(btnObj) {
	jQuery(btnObj).attr("disabled", true);
}

/**
 * 设置按钮可用
 * @param btnId
 */
function setButtonEnable(btnObj) {
	jQuery(btnObj).removeAttr("disabled");

}

/**
 * 设置按钮状态
 * @param status
 */
function setButtonStatus(status) {
	setAllButtonEnable();
	if (status == "add" || status == "edit") {
		setButtonDisAble("btnAdd");
		setButtonDisAble("btnEdit");
		setAllPageEnable()

	} else if (status == "edit") {
		setButtonDisAble("btnAdd");
		setButtonDisAble("btnEdit");
		setAllPageEnable()
	} else if (status == "save") {
		setButtonDisAble("btnSubmit");
		setButtonDisAble("btnCancel");
		setAllPageDisable();
	} else if (status == "init") {
		setButtonDisAble("btnSubmit");
		setButtonDisAble("btnCancel");
		setAllPageDisable();
	} else {
		setButtonDisAble("btnSubmit");
		setButtonDisAble("btnCancel");
		setAllPageDisable();
		if ("${not empty category.id?'1':'0'}" == '0') {
			setButtonDisAble("btnEdit");
		}
	}

}


//设置页面不可编辑
function setAllPageDisable(formArea) {
	jQuery("input", formArea).attr("disabled", true);
	jQuery("textarea", formArea).attr("readonly", "readonly");

	jQuery(".aBtn", formArea).css('display', 'none');
	jQuery(".btn", formArea).css('display', 'none');
	jQuery(".select2-arrow", formArea).css('display', 'none');

	jQuery(".select2-offscreen", formArea).attr("disabled", true);
	jQuery("img", formArea).next('a').css('display','none');
	jQuery("li a[href='javascript:']", formArea).css('display','none');
}

//设置页面可编辑
function setAllPageEnable(formArea) {
	jQuery("input", formArea).removeAttr("disabled");
	jQuery("textarea", formArea).removeAttr("readonly");
	
	jQuery(".aBtn", formArea).css('display', '');
	jQuery(".btn", formArea).css('display', '');
	jQuery(".select2-arrow", formArea).css('display', '');

	jQuery(".select2-offscreen", formArea).removeAttr("disabled");
	jQuery("img", formArea).next('a').css('display','');
	jQuery("li a[href='javascript:']", formArea).css('display','');
}

/**
 * 打开剪裁窗口
 * @param obj
 * @param inputId
 */
function openCropWindow(obj,inputId,url){
	if(url == null || url == ''){
		url = "/soccercms/a/cms/imageinfo/imageView";
	}
	var imgObj = jQuery(obj).parent().find('ol li img')[0];
	var imgUrl = jQuery(imgObj).attr('url');
	if(imgUrl == null || imgUrl == '' ){
		alertx("请选择图片");
		return ;
	}  //debugger;
	if(inputId == null){
		inputId = jQuery(obj).parent().find('input[type="hidden"][class*="input"]').attr('id');
	}
	imgUrl = encodeURIComponent(imgUrl);
	url = url+'?imgUrl='+imgUrl;
	url = url+"&inputId="+inputId;
	windowOpen(url,"图片裁剪",1200,800);
	
}

/**
 * 更新图片路径
 * @param id
 * @param path
 */
function updateImagePath(id,path){
	var inputObj = jQuery('#'+id);
	inputObj.val(path);
	//debugger;
	var divObj = inputObj.parents('div')[0];
	
	jQuery('#'+id+'Preview',divObj).children().remove();
	var li = "<li><img src=\""+path+"\" url=\""+path+"\" style=\"max-width:100px;max-height:100px;_height:100px;border:0;padding:3px;\">";//
	li += "&nbsp;&nbsp;<a href=\"javascript:\" onclick=\"nameImageDel(this);\">×</a></li>";
	jQuery('#'+id+'Preview',divObj).append(li);
	var imgObj = jQuery('#'+id+'Preview',divObj).find('img');
	imgObj.attr('src',path);
	imgObj.attr('url',path);
}

/**
 * 缩放图片路径
 * @param obj
 */
function zoomImage(obj){
	var imgObj = jQuery(obj).parent().find('ol li img')[0];
	var imgUrl = jQuery(imgObj).attr('url');
	if(imgUrl == null || imgUrl == '' ){
		alertx("请选择图片");
		return ;
	}
	jQuery('[name="extImage"]').each(function(i,v){
		var inputId =  jQuery(this).find('input[type="hidden"][class*="input"]').attr('id');
		updateImagePath(inputId,imgUrl);
	});
	
}

//获取表单数据
function getFormJsonData(formObj){
	var form = jQuery(formObj)[0];
	var data = {}; 
	if(form!=null&&form.length>0){
		var filterType = ""
		for(var i = 0 ; i < form.length ; i++ ){
			var e = form[i];
			var key = e.name;
			var value = e.value;
			if(key==null || key == ''){
				continue;
			}
			data[key] = value;
		}
	}
	return data;
}

//获取表单数据
function getFormData(formObj){
	var form = jQuery(formObj)[0];
	var data = {}; 
	if(form!=null&&form.length>0){
		var filterType = ""
		for(var i = 0 ; i < form.length ; i++ ){
			var e = form[i];
			var key = e.name;
			var value = e.value;
			if(key==null || key == '' ||value == null ||value == ''){
				continue;
			}
			var input = {};
			input.key = key;
			input.value = value;
			if(e.tagName=='INPUT'){
				input.label = value;
			}else if(e.tagName == 'SELECT'){
				input.label = jQuery(e).find("option:selected").text();
			}else if(e.tagName == "TEXTAREA"){
				input.label = value;
			}else{
				input.label = value;
			}
			input.tagName = e.tagName;
			data[key] = input;
		}
	}
	return data;
}

function getDataByElment(obj){
	var data = {};
	jQuery(obj).find('input').each(function(i,e){
		var name = e.name;
		var key = name.substr(name.indexOf(".")+1);
		data[key]=e.value;
		
	});
	return data;
}

function jsonToUrlParam(data,encode){
	var url = "";
	var array = [];debugger;
	if(encode==null || encode == false){
		for(var i in data){
			array.push(i+'='+data[i]);
		}
	}else{
		for(var i in data){
			array.push(i+'='+encodeURI(encodeURI(data[i])));
		}
	}
	
	url = array.join('&');
	return url;
}
